import {Command} from '../constant';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import api from 'api';
import {extraPathsValue} from '@/redux/util';
import Taro from '@tarojs/taro';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async getGameUserInfo() {
      const gameBase = await api.MemberController.getIplandMemberBase();
      console.log(gameBase, '我是用户信息');
      if (gameBase.nickName) {
        action.commonChange('main.customer.name', gameBase.nickName);
      }

      if (gameBase.headPortrait) {
        action.commonChange('main.customer.pictureUrl', gameBase.headPortrait);
      }
    },
    async getMemberBase() {
      console.log('进来了');
      const result = await api.MemberController.getMemberBase();
      action.commonChange('main.memberInfo', result);
    },
    async getCollectBase() {
      const result = await api.CollectionCabineController.getCollectionList();
      action.commonChange('main.collectionList', result.slice(0, 4));
    },

    /**
     * 查询我的许愿池
     * @param payload
     */
    async loadMyWishItems(payload) {
      action.commonChange('main.myWishItems', []);
      action.commonChange('main.response.loadWish', '');
      action.commonChange('main.status.loadingWish', true);

      // 编辑参数
      let params = {
        ...payload,
      };

      let {data, message, code} = await api.WishBaseController.getUserWishItemsList(params);
      console.log('action.ts loadmyWishItems data', data);

      let result = data || [];

      action.commonChange('main.myWishItems', result);
      action.commonChange('main.response.loadWish', code);
      action.commonChange('main.status.loadingWish', false);
    },

    /**
     * 查询推荐商品
     */
    async fetchrecommendation() {
      action.commonChange('main.productList', []);
      action.commonChange('main.productListPageNum', 0);

      let {
        main: {productListPageNum, productList},
      } = getData();
      productListPageNum = productListPageNum + 1;

      let {content} = await api.wechatLoginController.recommendmodule({
        pageNum: 0,
        pageSize: 1000,
        moduleType: 0, // 许愿池模块
      });
      content = content || [];
      productList = [...productList, ...content];
      action.commonChange('main.productListPageNum', productListPageNum);
      action.commonChange('main.productList', productList);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageIPersonalCenterMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
